﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.WebModel;
using System.Web.Services;
using System.Web.Script.Services;
using TrainingsApp.Helper;
using TrainingsApp.Model;
using TrainingsApp.Logic;

namespace TrainingsApp.IPADApp.Teacher
{
    public partial class PreviewQuickQuery : AccountPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override WebModel.Other.AccountMenuType MenuType
        {
            get { return WebModel.Other.AccountMenuType.Preview; }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
        public static string GetPriviewList(long ChapterId, int iPageIndex, int iPageSize)
        {
            int iRowCount, iPageCount = -1;
            bool boolIsSucess = true;
            string strBackMessage = string.Empty;
            TS_Preview_Collecion pList = null;
            if (PassPort.IsAuthenticated)
            {
                try
                {
                    Dictionary<string, object> dict = new Dictionary<string, object>();
                    dict.Add("UserId", PassPort.CurrentUser.UserID);
                    dict.Add("ChapterId", ChapterId);
                    pList = TS_Preview_Logic.TS_Preview_QuickQueryListByPaging(dict, iPageSize, iPageIndex);
                    if (pList != null && pList.TS_Preview_List.Count > 0)
                    {
                        foreach (TS_Preview_Entity entity in pList.TS_Preview_List)
                        {
                            entity.ReferencePercent = Math.Round((entity.ReferenceCount * 100.0 / (entity.ReferenceTotal == 0 ? 1 : entity.ReferenceTotal)), 2);
                            entity.resultList = TS_Preview_Result_Logic.TS_Preview_Result_GetTypicalByPreviewId(entity.PreviewId.Value);
                            if (entity.resultList != null && entity.resultList.Count > 0)
                            {
                                foreach (TS_Preview_Result_Entity r in entity.resultList)
                                {
                                    r.ReferencePercent = Math.Round((r.GroupCount * 100.0 / (r.GroupTotal == 0 ? 1 : r.GroupTotal)), 2);
                                }
                            }
                        }
                    }
                    iRowCount = pList.TotalCount;
                    iPageCount = pList.TotalCount % iPageSize == 0 ? pList.TotalCount / iPageSize : pList.TotalCount / iPageSize + 1;
                }
                catch (Exception ex)
                {
                    boolIsSucess = false;
                    strBackMessage = ex.Message.Replace("\r", "").Replace("\n", "");
                }
            }
            else
            {
                boolIsSucess = false;
                strBackMessage = "您已经登录超时，请重新登录系统。";
            }

            return "{" + string.Format("IsSuccess:{0},Msg:'{1}',PageCount:{2},Table:{3}", boolIsSucess.ToString().ToLower(), strBackMessage, iPageCount, JsonHelper.ToStdJson(pList)) + "}";
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
        public static string PreviewAdd(string Question, long ChapterId)
        {
            bool boolIsSucess = true;
            string strBackMessage = string.Empty;
            string PreviewId = string.Empty;
            string QuestionId = string.Empty;
            TS_Preview_Entity entity = new TS_Preview_Entity();
            TS_User_Teacher_Entity c = new TS_User_Teacher_Entity();
            if (PassPort.IsAuthenticated)
            {
                try
                {
                    TS_TeachMaterials_Chapter_Entity tc = TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_View(ChapterId);
                    int refCount = 0;
                    TS_Question_Entity q = TS_Question_Logic.TS_Question_Add(new TS_Question_Entity()
                    {
                        QuestionId = Guid.NewGuid(),
                        ChapterId = ChapterId,
                        CourseId = tc.CourseId,
                        MaterialsId = tc.MaterialsId,
                        Question = Question,
                        QuestionType = 2
                    });
                    QuestionId = q.QuestionId.Value.ToString();

                    c = TS_User_Teacher_Logic.TS_User_Teacher_View(PassPort.CurrentUser.UserID).First();

                    entity = TS_Preview_Logic.TS_Preview_Add(new TS_Preview_Entity()
                    {
                        PreviewId = Guid.NewGuid(),
                        ChapterId = ChapterId,
                        ClassId = c.ClassesId,
                        CourseId = tc.CourseId,
                        MaterialsId = tc.MaterialsId,
                        PublishDate = DateTime.Now,
                        Question = Question,
                        QuestionId = new Guid(QuestionId),
                        ReferenceCount = 0,
                        SchoolId = c.SchoolId,
                        TeachId = PassPort.CurrentUser.UserID
                    });
                    PreviewId = entity.PreviewId.Value.ToString();
                }
                catch (Exception ex)
                {
                    boolIsSucess = false;
                    strBackMessage = ex.Message.Replace("\r", "").Replace("\n", "");
                }
            }
            else
            {
                boolIsSucess = false;
                strBackMessage = "您已经登录超时，请重新登录系统。";
            }
            return "{" + string.Format("IsSuccess:{0},Msg:'{1}',PreviewId:'{2}',Name:'{3}'", boolIsSucess.ToString().ToLower(), strBackMessage, PreviewId, c.Name) + "}";
        }
    }
}